local Lua__haha = fk.CreateSkill({
  name = "Lua__haha",
  tags = { Skill.Limited },
})

Fk:loadTranslationTable {
  ["Lua__haha"] = "哈哈",
  [":Lua__haha"] = "限定技，当你累积使用过四张花色不同的牌后，你可清除记录的所有花色并对一名非蜀势力角色依次造成X点、Y点伤害，然后你的所有技能视为未发动过并摸4-X张牌（X为你手牌中的花色数且至少为1，Y为你计算与其的距离且至多为5）。",
  ["@Lua__haha"] = "哈哈",
  ["#Lua__haha-choose"] = "哈哈：选择一名非蜀势力角色，依次对其造成对应点伤害！",
  ["$Lua__haha1"] = "哈哈哈哈哈哈哈哈哈哈哈哈",
  ["$Lua__haha2"] = "哈哈哈哈哈哈哈哈哈哈哈哈",
}

Fk:addTargetTip{
  name = "Lua__haha",
  target_tip = function (self, player, to_select, selected, selected_cards, card, selectable, extra_data)
    if to_select == player or to_select.kingdom == "shu" then return end
    local suits = {}
    for _, id in ipairs(player:getCardIds("h")) do
      table.insertIfNeed(suits, Fk:getCardById(id).suit)
    end
    local x = math.max(#suits, 1)
    local y = math.min(player:distanceTo(to_select), 5)
    return x.."+"..y
  end,
}

Lua__haha:addEffect(fk.AfterCardUseDeclared, {
  mute = true,
  is_delay_effect = true,
  can_trigger = function(self, event, target, player, data)
    return target == player and player:hasSkill(Lua__haha.name) and not table.contains(player:getTableMark("@Lua__haha"), data.card:getSuitString(true))
  end,
  on_use = function(self, event, target, player, data)
    player.room:addTableMark(player, "@Lua__haha", data.card:getSuitString(true))
  end,
})

Lua__haha:addEffect(fk.CardUsing, {
  can_trigger = function(self, event, target, player, data)
    return target == player and player:hasSkill(Lua__haha.name) and #player:getTableMark("@Lua__haha") >= 4 and player:usedEffectTimes(self.name, Player.HistoryGame) == 0
  end,
  on_cost = function(self, event, target, player, data)
    local room = player.room
    local targets = table.filter(room.alive_players, function(p) return p.kingdom ~= "shu" end)
    local to = room:askToChoosePlayers(player, {
      targets = targets,
      min_num = 1,
      max_num = 1,
      prompt = "#Lua__haha-choose",
      skill_name = Lua__haha.name,
      cancelable = true,
      target_tip_name = "Lua__haha",
    })
    if #to > 0 then
      event:setCostData(self, { target = to[1] })
      return true
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    room:setPlayerMark(player, "@Lua__haha", 0)
    local to = event:getCostData(self).target
    local suits = {}
    for _, id in ipairs(player:getCardIds("h")) do
      table.insertIfNeed(suits, Fk:getCardById(id).suit)
    end
    local x = math.max(#suits, 1)
    local y = math.min(player:distanceTo(to), 5)
    room:damage {
      from = player,
      to = to,
      damage = x,
      skillName = Lua__haha.name,
    }
    if not to.dead then
      room:damage {
        from = player,
        to = to,
        damage = y,
        skillName = Lua__haha.name,
      }
    end
    local skills = table.filter(player:getSkillNameList(), function(s) return Fk.skills[s]:isPlayerSkill(player) end)
    if #skills > 0 then
      for _, s in ipairs(skills) do
        player:clearSkillHistory(s)
      end
      room:sendLog {
        type = "%from 的所有技能已被【哈哈】重置！",
        from = player.id,
      }
    end
    local now_suits = {}
    for _, id in ipairs(player:getCardIds("h")) do
      table.insertIfNeed(now_suits, Fk:getCardById(id).suit)
    end
    x = 4 - math.max(#now_suits, 1)
    player:drawCards(x, Lua__haha.name)
  end,
})


return Lua__haha
